﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DHLPricing.BaseInfo
{
    public partial class EditCustomer : Form
    {
        public List<Service.Customers.Customer> AllCustomers = new List<Service.Customers.Customer>();
        Service.Customers.Customer customer = new Service.Customers.Customer();
        Service.Customers.CustomerSoapClient CustomerService = new Service.Customers.CustomerSoapClient();

        public EditCustomer(string code, List<Service.Customers.Customer> allCustomers)
        {
            AllCustomers = allCustomers;

            var c = (from info in AllCustomers
                        where info.Code == code
                        select info);
            if (c != null && c.Count() > 0)
            {
                customer = c.First();
            }
            else
            {
                customer = null;
            }

            InitializeComponent();
        }

        private void AddCustomer_Load(object sender, EventArgs e)
        {
            if (customer == null)
            {
                MessageBox.Show("客户信息不存在!");
            }
            

            lblErr.Text = "";

            DataTable dtType = new DataTable();
            dtType.Columns.Add("Value");
            dtType.Columns.Add("Name");
            DataRow drType;
            drType = dtType.NewRow();
            drType[0] = "1"; 
            drType[1] = "NO"; 
            dtType.Rows.Add(drType);
            drType = dtType.NewRow();
            drType[0] = "10"; 
            drType[1] = "VIP"; 
            dtType.Rows.Add(drType);

            cbType.DisplayMember = "Name";
            cbType.ValueMember = "Value";
            cbType.DataSource = dtType;

            txtCode.Text = customer.Code;
            txtCName.Text = customer.CName;
            cbType.SelectedValue = ((int)customer.Type).ToString();
            txtPhone.Text = customer.PhoneNumber;
            txtEmail.Text = customer.Email ;
            txtAddress.Text = customer.Address;
            txtNotes.Text = customer.Notes;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Save())
            {
                this.Close();
            }
            
        }

        bool Save()
        {
            string code = txtCode.Text.Trim().ToUpper();
            string cname = txtCName.Text.Trim();
            string type = cbType.SelectedValue.ToString();
            string phone = txtPhone.Text.Trim();
            string email = txtEmail.Text.Trim();
            string address = txtAddress.Text.Trim();
            string notes = txtNotes.Text.Trim();



            if (code.Equals(string.Empty))
            {
                lblErr.Text = "请填写客户编码!";
                txtCode.Focus();
                return false;
            }
            else
            {
                Service.Customers.Customer c = new Service.Customers.Customer();
                c.Code = code.ToUpper();
                c.Address = address;
                c.CName = cname;
                c.Email = email;
                c.Notes = notes;
                c.PhoneNumber = phone;
                c.Type = (Service.Customers.CustomerType)(int.Parse(type));

                string result = CustomerService.UpdateCustomer(c);
                if (result == "0")
                {
                    MessageBox.Show("更新成功!");
                    return true;
                }
                else
                {
                    MessageBox.Show(result);
                    return false;
                }
            }
        }

       
    }
}
